﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Xml.Serialization;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives;
using System.Text.RegularExpressions;

namespace Wordpress.Phone.Models
{
    public class Movie : INotifyPropertyChanged
    {        
        private DeliveryMethods _deliveryMethod;
        public DeliveryMethods DeliveryMethod
        {
            get
            {
                return this._deliveryMethod;
            }
            set
            {
                if (this._deliveryMethod != value) {
                    this._deliveryMethod = value;
                    this.OnNotifyPropertyChanged("DeliveryMethod");
                }
            }
        }
        
        private string _title = "";
        public string Title
        {
            get
            {
                return this._title;
            }
            set
            {
                if (value != this._title)
                {
                    this._title = value;
                    this.OnNotifyPropertyChanged("Title");
                }
            }
        }

        private string _description;

        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                if (value != this._description)
                {
                    this._description = value;
                    this.OnNotifyPropertyChanged("Description");
                }
            }
        }

        private Uri _source;

        [XmlIgnore]
        public Uri Source
        {
            get
            {
                return this._source;
            }
            set
            {
                if (value != this._source)
                {
                    this._source = value;
                    this.OnNotifyPropertyChanged("Source");
                }
            }
        }

        public string SourceString
        {
            get
            {
                return this.Source.ToString();
            }
            set
            {
                this.Source = new Uri(value);
            }
        }

        public bool IsThumbnail { get; set; }

        private Thumbnail _thumbnail;

        public Thumbnail Thumbnail
        {
            get
            {
                if (this.IsThumbnail == false) {
                    return new Thumbnail
                    {
                        Source = new Uri("/Wordpress.Phone;component/Assets/Images/Thumbnail.png", UriKind.Relative)
                    };

                }
                return this._thumbnail;
            }

            set
            {
                if (value != this._thumbnail) {
                    this._thumbnail = value;
                    this.IsThumbnail = true;
                    this.OnNotifyPropertyChanged("Thumbnail");
                }
            }
        }

        public static Movie detect (Uri url)
        {
            Movie m = new Movie();

            string pattern = @"\.ism[l]?/Manifest$";
            Regex urlregex = new Regex(pattern);

            if (urlregex.IsMatch(url.ToString())) {
                m.DeliveryMethod = (DeliveryMethods)DeliveryMethods.AdaptiveStreaming;
            }
            else {
                m.DeliveryMethod = (DeliveryMethods)DeliveryMethods.ProgressiveDownload;
            }
            return m;
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnNotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        #endregion
    }
}
